<?php

/**
 * class purchaseOrder
 *
 * Description for class purchaseOrder
 *
 * @author:peter.seter@dwebsite.com
*/
Zend_Loader::loadClass("CodesUtility");
Zend_Loader::loadClass("BaseDBRow");
class purchaseOrder extends BaseDBRow {

	const _tablename = "projecte_sales.orders";
	protected function setUp()
	{
		$this->_tableName = self::_tablename;		
	}
	
	public function getPurchaseOrder($org_id,$poid,$supplier_org_id) {
	
		Zend_Loader::loadClass("Organization");
		Zend_Loader::loadClass("OrganizationOrganism");
		Zend_Loader::loadClass("QueryCreator");
		Zend_Loader::loadClass("purchaseOrderDetail");
		Zend_Loader::loadClass("Organism");
		Zend_Loader::loadClass("Size");
		Zend_Loader::loadClass("ShipmentDetail");
		
		$query = new QueryCreator();
		$query->addSelect(" b.name,a.po_no,a.date_order ");
		$query->addFrom($this->_tableName . " a ");
		$query->addJoin(" LEFT JOIN ". Organization::_tablename . " b on a.org_id = b.org_id " );
		//$query->addJoin(" LEFT JOIN ". purchaseOrderDetail::_tablename . " c on c.po_id = a.po_id ");
		$query->addWhere(" a.org_id = ". $org_id );
		$query->addWhere(" a.po_id = ". $poid );
		//$query->addWhere(" c.supplier_org_id = ". $customer_org_id);
		//$query->addGroupBy("  ");
		
		$db = DBCon::instance();
		//echo $query->createSQL();
		$result = $db->executeQuery($query->createSQL());
		$data = array();
		$data[0] = $result[0];
		
		$query2 = new QueryCreator();
		$query2->addSelect(" po_detail_id,c.size,moid,b.name as common_name,order_quantity,c.size_code,order_amount,b.scientific_name");
		$query2->addSelect(" a.order_quantity - ifnull((select sum(ship_quantity) from ". ShipmentDetail::_tablename ." shipment where shipment.po_line_no = a.po_detail_id ),0) as balance");
		$query2->addSelect("replace(b.scientific_name,' ','-') as image");
		$query2->addFrom(purchaseOrderDetail::_tablename . " a ");
		$query2->addJoin(" LEFT JOIN ". OrganizationOrganism::_tablename ." orgorg on orgorg.org_organism_id = a.org_organism_id ");
		$query2->addJoin(" LEFT JOIN  ". Organism::_tablename . " b on orgorg.organism_id  = b.organism_id " );
		$query2->addJoin(" LEFT JOIN ". Size::_tablename . " c on c.size_code=a.size_code");
		$query2->addWhere(" a.po_id  = ". $poid);
		$query2->addWhere(" a.supplier_org_id = ". $supplier_org_id );
		$query2->addHaving(" balance > 0");
		//echo $query2->createSQL();die();
		$result2 = $db->executeQuery($query2->createSQL());
		
		$data[0]['details'] = $result2;
		
		return $data;	
	}
	
	public function getPurchaseOrderHeader($poID, $orgID) {
	
		$query = new QueryCreator();
		$query->addSelect(" o.po_id,o.po_no,o.org_id ");
		$query->addSelect(" o.status,o.date_order ");
		$query->addFrom($this->_tableName . " o ");
		$query->addWhere(" o.po_id = ". $poID );
		
		$db = DBCon::instance();
		$result = $db->executeQuery($query->createSQL());
		
		return $result;
	}
}

?>